package org.sicau.votesys.service.imp;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.sicau.votesys.dao.VoteDao;
import org.sicau.votesys.domain.PO.SecondCandidatePO;
import org.sicau.votesys.domain.VO.FirstVoteResultVO;
import org.sicau.votesys.service.FileService;
import org.sicau.votesys.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author beifengtz
 * @Site www.beifengtz.com
 * @Date Created in 16:32 2018/11/5
 * @Description:
 */
@Service
public class FileServiceImp implements FileService {

    @Autowired
    private VoteDao voteDao;

    @Override
    public void excelTest(HttpServletRequest request, HttpServletResponse response) {
        // excel标题
        String[] title = {"名称","性别","年龄"};

        // excel文件名
        String fileName = "学生信息表"+System.currentTimeMillis()+".xls";

        // sheet名
        String sheetName = "学生信息表";

        String [][] content = {{"hahha","lalall","151"},{"lalalla","lolololo","222"}} ;

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName,title,content,null);

        try{
            this.setResponseHeader(response,fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try{
            fileName = new String(fileName.getBytes(),"utf-8");
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition","attachment;filename="+fileName);
            response.addHeader("Pargam","no-cache");
            response.addHeader("Cache-Control","no-cache");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    @Override
    public void downloadFirstVote(HttpServletRequest request, HttpServletResponse response) {
        List<FirstVoteResultVO> firstVoteResultVOList =  voteDao.queryFirstVoteResult();
        // excel标题
        String[] title = {"序号","选手ID","选手名","选手组别","得分（票数）"};

        // excel文件名
        String fileName = System.currentTimeMillis()+".xls";

        // sheet名
        String sheetName = "第一轮投票结果表";

        String [][] content = new String[firstVoteResultVOList.size()][5] ;
        for (int i = 0;i<firstVoteResultVOList.size();i++){
            content[i][0] = String.valueOf(i+1);
            content[i][1] = firstVoteResultVOList.get(i).getCandidateId();
            content[i][2] = firstVoteResultVOList.get(i).getCandidateName();
            content[i][3] = firstVoteResultVOList.get(i).getCollegeName();
            content[i][4] = String.valueOf(firstVoteResultVOList.get(i).getSum());
        }

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName,title,content,null);

        try{
            this.setResponseHeader(response,fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void downloadSecondVote(HttpServletRequest request, HttpServletResponse response) {
        List<SecondCandidatePO> secondCandidatePOList = voteDao.getSecondVoteData();
        // excel标题
        String[] title = {"序号","选手ID","选手名","学号","选手组别","得分（票数）"};

        // excel文件名
        String fileName = System.currentTimeMillis()+".xls";

        // sheet名
        String sheetName = "第二轮投票结果表";

        String [][] content = new String[secondCandidatePOList.size()][6] ;
        for (int i = 0;i<secondCandidatePOList.size();i++){
            content[i][0] = String.valueOf(i+1);
            content[i][1] = secondCandidatePOList.get(i).getId();
            content[i][2] = secondCandidatePOList.get(i).getCandidateName();
            content[i][3] = secondCandidatePOList.get(i).getSicauId();
            content[i][4] = secondCandidatePOList.get(i).getCollegeName();
            content[i][5] = String.valueOf(secondCandidatePOList.get(i).getVotedNum());
        }

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName,title,content,null);

        try{
            this.setResponseHeader(response,fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
